package eval

import (
	"errors"
	"fmt"
	"strings"

	"tvl.fyi/tvix/eval/internal/compiler"
	"tvl.fyi/tvix/eval/internal/runtime"
	"tvl.fyi/tvix/eval/parser"
	"tvl.fyi/tvix/eval/value"
)

func EvalNix(code string, strict bool) (value.NixValue, error) {
	code = strings.TrimSpace(code)
	if code == "" {
		return nil, errors.New("no Nix code provided")
	}

	parsed, err := parser.ParseString(code)
	if err != nil {
		return nil, fmt.Errorf("failed to parse code: %w", err)
	}

	chunk, err := compiler.Compile(parsed)
	if err != nil {
		return nil, fmt.Errorf("failed to compile code: %w", err)
	}

	return runtime.RunChunk(chunk, strict)
}

// Display returns a human-readable string serialisation of any Nix value.
var Display = runtime.Display
